package com.lxd.utils;

import com.lxd.common.constants.CacheConstant;
import com.lxd.common.constants.RabbitMQConstants;
import com.lxd.common.constants.SmsConstant;
import com.lxd.common.enums.ExceptionEnums;
import com.lxd.common.model.StandardReport;
import com.lxd.common.model.StandardSubmit;
import com.lxd.feign.cacheClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Dongdong
 * @注释 策略模块校验失败，进行的处理
 */
@Component
@Slf4j
public class ErrorSendMsgUtil {
    @Autowired
    private cacheClient cacheClient;

    @Autowired
    private RabbitTemplate mq;

    /**
     * 策略模块校验未通过，发送写日志操作
     *
     * @param submit
     * @param dirtyWords
     */
    public void sendWriteLog(StandardSubmit submit, List<String> dirtyWords) {
        // 提取并设置错误信息
        String errorMsg = ExceptionEnums.HAVE_DIRTY_WORD + "敏感词信息为:" + dirtyWords;
        submit.setErrorMsg(errorMsg);
        //设置短信发送状态
        submit.setReportState(SmsConstant.REPORT_FAIL);
        //发送至队列
        mq.convertAndSend(RabbitMQConstants.SMS_WRITE_LOG, submit);

    }
    public void sendWriteLog(StandardSubmit submit) {

        //设置短信发送状态
        submit.setReportState(SmsConstant.REPORT_FAIL);
        //发送至队列
        mq.convertAndSend(RabbitMQConstants.SMS_WRITE_LOG, submit);

    }

    /**
     * 策略模块校验未通过，发送状态报告操作
     */
    public void sendPushReport(StandardSubmit submit) {
        //给客户通知状态报告
        // 查询是否需要回调
        int isCallBack = (int) cacheClient.hget(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), "isCallback");
        ;
        log.info("【发送状态报告】客户的回调标志是 ={}", isCallBack);
        //需要回调
        if (isCallBack == 1) {
            //查询回调地址
            String url = cacheClient.hgetString(CacheConstant.CLIENT_BUSINESS + submit.getApikey(), "callbackUrl");//得到回调地址
            log.info("【发送状态报告】客户的回调地址={}",url);
            //回调地址不为空
            if (!StringUtils.isEmpty(url)) {
                StandardReport report = new StandardReport();
                BeanUtils.copyProperties(submit, report);//将一些相同属性的值进行赋值！
                report.setIsCallback(isCallBack);
                report.setCallbackUrl(url);
                //发送至mq，交给推送模块去通知报告
                mq.convertAndSend(RabbitMQConstants.SMS_PUSH_REPORT, report);
                log.info("【发送状态报告】已发送至{}队列",RabbitMQConstants.SMS_PUSH_REPORT);
            }
        }
    }
}
